#include "persondatacollector.h"

#include <QDialogButtonBox>
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QRegExpValidator>

PersonDataCollector::PersonDataCollector( QWidget *parent )
    : QDialog( parent )
{
	m_pButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok |
	                                     QDialogButtonBox::Cancel );

	m_pPersonIdLabel = new QLabel( "ID" );
	m_pPersonNameLabel = new QLabel( "Name" );

	m_pPersonIdText = new QLineEdit;
	m_pPersonNameText = new QLineEdit;

	QRegExp idRegExp( "^([0-9]+)$" );
	m_pPersonIdText->setValidator( new QRegExpValidator( idRegExp, this ) );

	QRegExp nameRegExp( "^([a-zA-Z ]+)$" );
	m_pPersonNameText->setValidator( new QRegExpValidator( nameRegExp, this ) );

	m_pLayout = new QGridLayout;
	m_pLayout->addWidget( m_pPersonIdLabel, 0, 0 );
	m_pLayout->addWidget( m_pPersonNameLabel, 1, 0 );
	m_pLayout->addWidget( m_pPersonIdText, 0, 1 );
	m_pLayout->addWidget( m_pPersonNameText, 1, 1 );
	m_pLayout->addWidget( m_pButtonBox, 3, 0, 1, 2 );

	connect( m_pButtonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
	connect( m_pButtonBox, SIGNAL( rejected() ), this, SLOT( reject() ) );

	setLayout( m_pLayout );
}

Person PersonDataCollector::getPerson()
{
	return m_person;
}

void PersonDataCollector::accept()
{
	m_person.setId( m_pPersonIdText->text().toInt() );
	m_person.setName( m_pPersonNameText->text() );

	done( Accepted );
}

void PersonDataCollector::reject()
{
	m_person = Person();

	done( Rejected );
}
